﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;

using ERPStore.Web.Routing;
using ERPStore.Extensions;
using ERPStore.Validators;

using Microsoft.Practices.Unity;

using FluentValidation;

namespace ERPStore.QuoteCart
{
	public class PluginInit : ERPStore.Extensibility.PluginInit
	{
		public PluginInit()
		{
			Container = GlobalConfiguration.Configuration.DependencyResolver.GetContainer();
		}

		Microsoft.Practices.Unity.IUnityContainer Container { get; set; }

		public override void AddServices()
		{
			var settings = new Models.Settings();
			settings.BindFromConfiguration(ERPStore.QuoteCart.Configuration.ConfigurationSettings.AppSettings);
			Container.RegisterInstance<Models.Settings>("quotecartsettings", settings, new ContainerControlledLifetimeManager());

			if (settings.RepositoryName == "sql")
			{
				Container.RegisterType<CloudSoft.Repositories.IRepositoryInitializer, CloudSoft.Repositories.Initializers.SqlSchemaInitializer<Repositories.QuoteCartDbContext>>("quotecartschemainitializer", new PerResolveLifetimeManager());
				Container.RegisterType<CloudSoft.Repositories.IDbContextFactory<Repositories.QuoteCartDbContext>, Repositories.DbContextFactory>(new PerThreadLifetimeManager());
				Container.RegisterType<Repositories.QuoteCartDbContext>(new PerThreadLifetimeManager());
				Container.RegisterType<Repositories.IQuoteCartRepository, Repositories.SqlQuoteCartRepository>(new Microsoft.Practices.Unity.ContainerControlledLifetimeManager());
			}
			else if (settings.RepositoryName == "mock")
			{
				Container.RegisterType<Repositories.IQuoteCartRepository, Repositories.MockQuoteCartRepository>(new ContainerControlledLifetimeManager());
			}

			// Services
			Container.RegisterType<Services.QuoteCartService>(new ContainerControlledLifetimeManager());

			var validatorService = Container.Resolve<ERPStore.Services.IValidatorService>();
			validatorService.RegisterValidator<Models.QuoteCart, ModelValidators.QuoteCartModelValidator>("quotecart");

		}

		public override void Load()
		{
			var settings = Container.Resolve<Models.Settings>("quotecartsettings");
			if (settings.RepositoryName == "sql")
			{
				var initializer = Container.Resolve<CloudSoft.Repositories.IRepositoryInitializer>("quotecartschemainitializer");
				initializer.Initialize("_erpstore_quotecart_schema");
			}


		}

		public override void RegisterRoutes()
		{
			RegisterQuoteCartRoutes(System.Web.Routing.RouteTable.Routes);
		}

		protected virtual void RegisterQuoteCartRoutes(System.Web.Routing.RouteCollection routes)
		{
			var namespaces = new string[] { typeof(Controllers.QuoteCartController).Namespace };

			routes.MapERPStoreRoute(
				Routes.QUOTECART_ADD
				, "panier-devis/ajouter"
				, new { controller = "QuoteCart", action = "AddItem" }
				, namespaces
			);

			routes.MapERPStoreRoute(
				Routes.QUOTECART_ITEM_DELETE
				, "panier-devis/supprimer/{index}"
				, new { controller = "QuoteCart", action = "Remove", index = 0 }
				, new { index = @"\d+" }
				, namespaces
			);

			routes.MapERPStoreRoute(
				Routes.QUOTECART_CLEAR
				, "panier-devis/vider"
				, new { controller = "QuoteCart", action = "Clear" }
				, namespaces
			);

			routes.MapERPStoreRoute(
				Routes.QUOTECART_DELETE
				, "panier-devis/supprimer/{cartId}"
				, new { controller = "QuoteCart", action = "Delete", cartId = string.Empty }
				, namespaces
			);

			routes.MapERPStoreRoute(
				Routes.QUOTECART_CHANGE
				, "panier-devis/changer/{cartId}"
				, new { controller = "QuoteCart", action = "Change", cartId = string.Empty }
				, namespaces
			);

			routes.MapERPStoreRoute(
				Routes.QUOTECART_SHOW
				, "panier-devis/voir/{cartId}"
				, new { controller = "QuoteCart", action = "Show", cartId = string.Empty }
				, namespaces
			);

			routes.MapERPStoreRoute(
				Routes.QUOTECART_CREATE
				, "panier-devis/creation"
				, new { controller = "QuoteCart", action = "Create" }
				, namespaces
			);

			routes.MapERPStoreRoute(
				Routes.QUOTECART_ADD_ITEM
				, "panier-devis/ajouter/{productCode}"
				, new { controller = "QuoteCart", action = "AddItem", productCode = string.Empty }
				, namespaces
			);

			routes.MapERPStoreRoute(
				Routes.JS_QUOTECART_ADD_ITEM
				, "panier-devis/jsadd/{productCode}"
				, new { controller = "QuoteCart", action = "JsAdd", productCode = string.Empty }
				, namespaces
			);

			routes.MapERPStoreRoute(
				Routes.JS_QUOTECART_ADD_ITEM_WITH_QUANTITY
				, "panier-devis/jsadd/{quantity}/{productCode}"
				, new { controller = "QuoteCart", action = "JsAddItemWithQuantity", quantity = 1, productCode = string.Empty }
				, namespaces
			);

			routes.MapERPStoreRoute(
				Routes.QUOTECART_STATUS
				, "panier-devis/status"
				, new { controller = "QuoteCart", action = "ShowStatus" }
				, namespaces
			);

			routes.MapERPStoreRoute(
				Routes.SHOW_ADD_TO_QUOTECART_POPUP
				, "panier-devis/popup-ajout"
				, new { controller = "QuoteCart", action = "ShowAddToCart" }
				, namespaces
			);

			routes.MapERPStoreRoute(
				Routes.SHOW_QUOTECART_STATUS
				, "panier-devis/statut"
				, new { controller = "QuoteCart", action = "ShowStatus" }
				, namespaces
			);

			routes.MapERPStoreRoute(
				Routes.AJAX_ADD_TO_QUOTECART
				, "panier-devis/ajax-ajouter"
				, new { controller = "QuoteCart", action = "JsAddItemWithQuantity" }
				, namespaces
			);

			routes.MapERPStoreRoute(
				Routes.QUOTECART_SCRIPT
				, "panier-devis/script"
				, new { controller = "QuoteCart", action = "Script" }
				, namespaces
			);

			routes.MapERPStoreRoute(
				Routes.QUOTECART
				, "panier-devis"
				, new { controller = "QuoteCart", action = "Index", id = string.Empty }
				, namespaces
			);

			routes.MapERPStoreRoute(
				Routes.QUOTECART_SEND
				, "panier-devis/transmis/{cartId}"
				, new { controller = "QuoteCart", action = "QuoteSend" }
				, namespaces
			);

			routes.MapERPStoreRoute(
				Routes.DEFAULT_QUOTECART
				, "panier-devis/{action}/{id}"
				, new { controller = "QuoteCart", action = "Index", id = "" }  // Parameter defaults
				, namespaces
			);
		}
	}
}
